import { NextRequest, NextResponse } from 'next/server';
import { CategoryService } from '@/app/lib/DataBase/database';

// 获取所有分类
export async function GET() {
  try {
    const categories = await CategoryService.getAllCategories();
    
    return NextResponse.json({ 
      success: true, 
      data: categories 
    });
  } catch (error) {
    console.error('获取分类列表失败:', error);
    return NextResponse.json({ 
      success: false, 
      message: '获取分类列表失败',
      error: error instanceof Error ? error.message : '未知错误'
    }, { status: 500 });
  }
}

// 添加新分类
export async function POST(request: NextRequest) {
  try {
    const { name, description } = await request.json();
    
    if (!name) {
      return NextResponse.json({ 
        success: false, 
        message: '缺少分类名称' 
      }, { status: 400 });
    }
    
    const result = await CategoryService.addCategory(name, description);
    
    return NextResponse.json({ 
      success: true, 
      data: result,
      message: '分类添加成功' 
    });
  } catch (error) {
    console.error('添加分类失败:', error);
    return NextResponse.json({ 
      success: false, 
      message: '添加分类失败',
      error: error instanceof Error ? error.message : '未知错误'
    }, { status: 500 });
  }
}
